package com.zhuozhou.jdyc.main.meinfo.activity;

import android.os.Bundle;
import android.view.View;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityWalletBinding;
import com.zhuozhou.jdyc.main.main.adapter.IndexTabMenuAdapter;
import com.zhuozhou.jdyc.main.main.bean.TabItemBean;
import com.zhuozhou.jdyc.main.meinfo.adapter.WalletAdapter;
import com.zhuozhou.jdyc.main.meinfo.bean.WalletBean;
import com.zhuozhou.jdyc.main.meinfo.viewmodel.MeInfoViewModel;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/9/22
 * Describe:我的钱包
 */
public class WalletActivity extends BaseActivity<MeInfoViewModel, ActivityWalletBinding> {

    private List<TabItemBean> tabMenuList = new ArrayList<>();
    private IndexTabMenuAdapter indexTabMenuAdapter;
    private WalletAdapter walletAdapter;
    private int type = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet;
    }

    @Override
    public String setTitleBar() {
        return "我的钱包";
    }//足迹 spoor 收藏 collect

    @Override
    public void initView() {
        binding.llWithdrawal.setOnClickListener(v -> {
            binding.vBottom1.setVisibility(View.GONE);
            binding.vBottom0.setVisibility(View.VISIBLE);
            binding.llMoney.setVisibility(View.VISIBLE);
            binding.llMx.setVisibility(View.GONE);
            binding.rlTx.setVisibility(View.VISIBLE);
        });
        binding.llStatement.setOnClickListener(v -> {
            binding.vBottom1.setVisibility(View.VISIBLE);
            binding.vBottom0.setVisibility(View.GONE);
            binding.llMoney.setVisibility(View.GONE);
            binding.llMx.setVisibility(View.VISIBLE);
            binding.rlTx.setVisibility(View.GONE);
        });
        mViewModel.BasicsStatisticsMutableLiveData.observe(this, v -> {
            binding.setInfo(v);
        });

        tabMenuList = new ArrayList<>();
        tabMenuList.add(new TabItemBean(0, "全部", baseBundle.getInt("type") == 0));
        tabMenuList.add(new TabItemBean(1, "待提成", baseBundle.getInt("type") == 1));
        tabMenuList.add(new TabItemBean(2, "积分", baseBundle.getInt("type") == 2));
        tabMenuList.add(new TabItemBean(3, "易货积分", baseBundle.getInt("type") == 3));
        tabMenuList.add(new TabItemBean(4, "已提成", baseBundle.getInt("type") == 4));

        mViewModel.BasicsWalletMutableLiveData.observe(this, v -> {
            indexTabMenuAdapter = new IndexTabMenuAdapter(this);
            indexTabMenuAdapter.setViewWidth(binding.llTop.getWidth() / 5);
            indexTabMenuAdapter.setTextColor(R.color.grey_700);
            indexTabMenuAdapter.setList(tabMenuList);
            indexTabMenuAdapter.setOnTabMenuItemClickListener((view, position) -> {
                type = position;
                mViewModel.getBasicsWallet(position, UserAccountHelper.getUserID() + "");
            });
            binding.mRecyclerViewTopMenu.setAdapter(indexTabMenuAdapter);
            binding.mRecyclerViewTopMenu.setLayoutManager(new FullyGridLayoutManager(this, 5) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            setWalletAdapter(v.getList());
        });
        binding.tvWithdrawal.setOnClickListener(v -> {
            if (StringUtil.isEmpty(binding.proDescribe.getText().toString())) {
                ToastUtils.showShort("请输入提现金额");
                return;
            }
            mViewModel.postMoney(binding.proDescribe.getText().toString(), UserAccountHelper.getUserID());
        });

        mViewModel.MoneyWalletMutableLiveData.observe(this, v -> {
            if (v.getType() == 1)
                ToastUtils.showShort("提现成功");
        });
    }

    public void setWalletAdapter(List<WalletBean.ListBean> list) {
        if (list != null && list.size() > 0) {
            binding.tvNoDetail.setVisibility(View.GONE);
            binding.mRecyclerViewWallet.setVisibility(View.VISIBLE);
            walletAdapter = new WalletAdapter(this);
            walletAdapter.setType(type);
            walletAdapter.setList(list);
            binding.mRecyclerViewWallet.setAdapter(walletAdapter);
            binding.mRecyclerViewWallet.setLayoutManager(new MyLayoutManager(this));
        } else {
            binding.tvNoDetail.setVisibility(View.VISIBLE);
            binding.mRecyclerViewWallet.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getBasicsWallet(type, UserAccountHelper.getUserID() + "");
        mViewModel.getBasicsStatistics(UserAccountHelper.getUserID() + "");
    }
}
